package com.hzo.great.leisuregreat.views;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hzo.great.leisuregreat.R;
import com.hzo.great.leisuregreat.base.BaseActivity;
import com.hzo.great.leisuregreat.constants.UrlConstant;
import com.hzo.great.leisuregreat.dialog.LoadingDialog;
import com.hzo.great.leisuregreat.utils.MyStringCallback;
import com.hzo.great.leisuregreat.utils.RequestSever;
import com.hzo.great.leisuregreat.utils.SharedPreferencesUtils;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends BaseActivity {

    TextView regist, login, forgetPwd;

    ImageView close;

    EditText phoneNum, password;

    String phoneNumber, pwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //fullScreen(this, true);
        setContentView(R.layout.login);
        initView();
    }

    private void initView() {

        regist = (TextView) findViewById(R.id.regist);
        login = (TextView) findViewById(R.id.login);
        forgetPwd = (TextView) findViewById(R.id.forgetPwd);

        close = (ImageView) findViewById(R.id.close);

        phoneNum = (EditText) findViewById(R.id.phoneNum);
        password = (EditText) findViewById(R.id.password);

        regist.setOnClickListener(this);
        login.setOnClickListener(this);
        close.setOnClickListener(this);
        forgetPwd.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.regist:
                startActivity(new Intent(LoginActivity.this,RegistActivity.class));
                finish();
                break;
            case R.id.login:
                phoneNumber = phoneNum.getText().toString();
                pwd = password.getText().toString();
                //Toast.makeText(this,"手机号：" + phoneNumber + "    密码：" + pwd,Toast.LENGTH_SHORT).show();

                if (phoneNumber.equals("") || !phoneNumber.startsWith("1") || phoneNumber.length() != 11){
                    ShortToast("请输入正确手机号");
                    return;
                }

                if (pwd.equals("")){
                    ShortToast("请输入登录密码");
                    return;
                }

                if (pwd.length() < 6){
                    ShortToast("密码不能低于6位");
                    return;
                }

                login();
                break;
            case R.id.close:
                LoginActivity.this.finish();
                break;
            case R.id.forgetPwd:
                startActivity(new Intent(LoginActivity.this,ModifyPasswordActivity.class));
                finish();
                break;
        }
    }

    private void login() {
        Map<String, Object> map = new HashMap<>();
        map.put("phoneNo", phoneNumber);
        map.put("loginPwd",pwd);
        //Toast.makeText(this,"手机号：" + phoneNumber + "    密码：" + pwd + "   登录中...",Toast.LENGTH_SHORT).show();
        LoadingDialog.getInstance(this).showLoadDialog("");
        RequestSever.post(UrlConstant.LOGIN_URL,map,new MyStringCallback<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LoadingDialog.closeLoadDialog();
                JSONObject jsonObject = JSON.parseObject(result);
                switch (jsonObject.getIntValue("code")){
                    case 200:
                        Map<String,String> data = JSON.parseObject(jsonObject.getString("data"),new TypeReference<Map<String, String>>(){});
                        if (data == null){
                            ShortToast("数据空异常!");
                            return;
                        }
                        SharedPreferencesUtils.commitString("access_token",data.get("access_token"));
                        SharedPreferencesUtils.commitString("customerId",data.get("customerId"));
                        SharedPreferencesUtils.commitString("phoneNumber",phoneNumber);
                        startActivity(new Intent(LoginActivity.this,MainActivity.class));
                        finish();
                        break;

                        default:
                            ShortToast(jsonObject.getString("message"));
                            break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                LoadingDialog.closeLoadDialog();
            }
        });
    }

    public void ShortToast(String str){
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }

}
